package com.example.express;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.express.adapter.QueryAdapter;
import com.example.express.base.BaseActivity;
import com.example.express.data.Constant;
import com.example.express.entity.KuaiDi;
import com.example.express.utlis.DialogUtil;
import com.example.express.utlis.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import static com.example.express.utlis.ToIntent.toIntentDIAL;

public class QueryActivity extends BaseActivity {

    private List<KuaiDi> kuaiDis = new ArrayList<>();
    private QueryAdapter queryAdapter;
    public static final int REQUEST_CALL_PERMISSION = 10111;
    private DialogUtil.Listener listener= new DialogUtil.Listener() {
        @Override
        public void item(String phone) {
            call(phone);
        }
    };

    public static void start(Context context){
        context.startActivity(new Intent(context,QueryActivity.class));
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_query;
    }

    @Override
    protected void initData() {
        kuaiDis.add(new KuaiDi(Constant.SF_NAME,Constant.SF_IMG,Constant.SF_PHONE,Constant.SF_URL));
        kuaiDis.add(new KuaiDi(Constant.ST_NAME,Constant.ST_IMG,Constant.ST_PHONE,Constant.ST_URL));
        kuaiDis.add(new KuaiDi(Constant.YD_NAME,Constant.YD_IMG,Constant.YD_PHONE,Constant.YD_URL));
        kuaiDis.add(new KuaiDi(Constant.ZT_NAME,Constant.ZT_IMG,Constant.ZT_PHONE,Constant.ZT_URL));
        kuaiDis.add(new KuaiDi(Constant.YT_NAME,Constant.YT_IMG,Constant.YT_PHONE,Constant.YT_URL));
        kuaiDis.add(new KuaiDi(Constant.BS_NAME,Constant.BS_IMG,Constant.BS_PHONE,Constant.BS_URL));
        kuaiDis.add(new KuaiDi(Constant.YZ_NAME,Constant.YZ_IMG,Constant.YZ_PHONE,Constant.YZ_URL));
        kuaiDis.add(new KuaiDi(Constant.EMS_NAME,Constant.EMS_IMG,Constant.EMS_PHONE,Constant.EMS_URL));
        kuaiDis.add(new KuaiDi(Constant.TT_NAME,Constant.TT_IMG,Constant.TT_PHONE,Constant.TT_URL));
        queryAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initView() {
        RecyclerView recycler = findViewById(R.id.recycler_view);
        queryAdapter = new QueryAdapter(kuaiDis, new QueryAdapter.QueryAdapterItemClick() {
            @Override
            public void item(String url,String title) {
                WebActivity.start(QueryActivity.this,url,title);
            }

            @Override
            public void itemPhone(String phone) {
                DialogUtil.show(QueryActivity.this,phone,listener);
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycler.setLayoutManager(manager);
        recycler.setAdapter(queryAdapter);
    }
    /**
     * 判断是否有某项权限
     * @param string_permission 权限
     * @param request_code 请求码
     * @return
     */
    public boolean checkReadPermission(String string_permission,int request_code) {
        boolean flag = false;
        if (ContextCompat.checkSelfPermission(this, string_permission) == PackageManager.PERMISSION_GRANTED) {
            flag = true;
        } else {//申请权限
            ActivityCompat.requestPermissions(this, new String[]{string_permission}, request_code);
        }
        return flag;
    }

    /**
     * 检查权限后的回调
     * @param requestCode 请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CALL_PERMISSION:
                if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    ToastUtil.show("请允许拨号权限后再试");
                }
                break;
        }
    }
    /**
     * 拨打电话
     * @param telPhone 电话
     */
    public void call(String telPhone){
        if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_CALL_PERMISSION)){
            toIntentDIAL(this,telPhone);
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                ToastUtil.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}